import index from '../components/index/Index'
import mine from '../components/mine/Mine'
import admin from '../components/admin/Admin'
import notFind from '../components/notFind/NotFind'
import userinfo from '../components/mine/UserInfo'
import message from '../components/mine/Message'
import manager from '../components/mine/Manager'
import store from '../vuex'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router =  new VueRouter({
    mode: 'history',
    routes:[
        {
            path:'/',
            name:'index',
            component:index
        },
        {
            path:'/mine',
            name:'mine',
            component:mine,
            children:[
                {
                    path:'info',
                    name:"info",
                    component: userinfo
                },
                {
                    path:'message',
                    name:"message",
                    component: message
                },
                {
                    path:'manager',
                    name:"manager",
                    component: manager
                }
            ]
        },
        {
            path:'/admin',
            name:'admin',
            component:admin
        },
        {
            path:'*',
            name:'notFind',
            component:notFind
        }
    ]
})

/**
 * 陆游拦截
 * 为登录用户，不允许访问用户后台
 */
router.beforeEach((to, from, next) => {
    if(to.matched[0].name == 'mine'){
        if(!store.state.user.isLogin){
            if(from.name == null){
                next('/');
            }else{
                next(false);
            }
        }
    }
    next();
})

export default router